package com.hirelinfo.form.admin;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hirelinfo.dto.faculty.User;
import com.hirelinfo.dto.student.Student;
import com.hirelinfo.service.faculty.UserManagerService;
import com.hirelinfo.service.student.StudentManagerService;

@Controller

public class Register {

@Autowired
StudentManagerService studentManagerService;
@Autowired
UserManagerService userManagerService;


	@RequestMapping("/registerPage")
	public ModelAndView disp() {
		ModelAndView mav = new ModelAndView("Register");
		
		mav.addObject("register", new User());

		return mav;

	}

	@RequestMapping(value = "/Register")
	public ModelAndView register(@Valid @ModelAttribute(value="register") User user, BindingResult result) {

		ModelAndView mav=new ModelAndView();

		

		if (result.hasErrors()) {
			mav.setViewName("Register");
			mav.addObject("register", user);
			System.out.println("Errror occurred");
			return mav;

		}
		
		
		userManagerService.saveNewUser(user);
		mav.setViewName("success");
		mav.addObject("user",user);

		mav.addObject("status", "registered successfully");

		System.out.println(user.getName());
		System.out.println(user.getPassword());
		return mav;
	}



	@RequestMapping("/studentRegisterpage")
	public ModelAndView studentForm() {
		ModelAndView mav = new ModelAndView("studentRegister");
		mav.addObject("registerForm", new Student());

		return mav;

	}

	@RequestMapping(value = "/StudentRegister")
	public ModelAndView registerForm(@Valid @ModelAttribute(value="registerForm") Student student, BindingResult result) {

		ModelAndView mav=new ModelAndView();
		if (result.hasErrors()) {
			mav.setViewName("studentRegister");
			mav.addObject("registerForm", student);
			System.out.println("Errror has been  occurred");
			return mav;

		}
		
		studentManagerService.saveNewStudent(student);
		
		mav.setViewName("studentSuccessPage");
		mav.addObject("student",student);

		mav.addObject("status", "Student Form successfully");


		return mav;
	}}
